package com.system.enterprisemanagementsystem.service.impl;

import com.system.enterprisemanagementsystem.mapper.ClazzMapper;
import com.system.enterprisemanagementsystem.mapper.EmpMapper;
import com.system.enterprisemanagementsystem.mapper.StudentMapper;
import com.system.enterprisemanagementsystem.pojo.JobOption;
import com.system.enterprisemanagementsystem.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClazzMapper clazzMapper;

    @Override
    public JobOption getEmpJobData() {
        List<Map<String,Object>> list = empMapper.countEmpJobData();
        List<Object> jobList = list.stream().map(dataMap -> dataMap.get("pos")).toList();
        List<Object> dataList = list.stream().map(dataMap -> dataMap.get("total")).toList();
        return new JobOption(jobList, dataList);
    }

    @Override
    public List<Map> getEmpGenderData() {
        return empMapper.countEmpGenderData();
    }


    public List<Map<String, Object>> getStudentDegreeData() {
        // 1. 从数据库查询各学历人数统计
        List<Map<String, Object>> degreeData = studentMapper.countStudentsByDegree();

        // 2. 定义学历映射关系
        Map<Integer, String> degreeMap = new HashMap<>();
        degreeMap.put(1, "初中");
        degreeMap.put(2, "高中");
        degreeMap.put(3, "大专");
        degreeMap.put(4, "本科");
        degreeMap.put(5, "硕士");
        degreeMap.put(6, "博士");

        // 3. 补全所有学历类型，并转换为要求的格式 ("name" 和 "value")
        List<Map<String, Object>> result = new ArrayList<>();

        for (int i = 1; i <= 6; i++) {
            // **使用要求的键名 "name" 和 "value"**
            Map<String, Object> degreeInfo = new HashMap<>();

            // 设置 name 键 (对应 degreeName)
            degreeInfo.put("name", degreeMap.get(i));

            // 默认 value 为 0
            Long countValue = 0L;

            // 查找实际统计结果
            for (Map<String, Object> data : degreeData) {
                // 假设数据库返回的 Key 是 "degree" 和 "count"
                Integer dataDegree = (Integer) data.get("degree");
                if (dataDegree != null && dataDegree == i) {
                    // 如果找到，更新 countValue
                    Object dbCount = data.get("count");
                    // 确保类型转换正确 (数据库可能返回 Integer, Long, 或 BigDecimal)
                    if (dbCount instanceof Number) {
                        countValue = ((Number) dbCount).longValue();
                    }
                    break;
                }
            }

            // 设置 value 键 (对应 count)
            degreeInfo.put("value", countValue);

            result.add(degreeInfo);
        }

        return result;
    }

    @Override
    public Map<String, Object> getStudentCountData() {
        List<Map<String, Object>> list = clazzMapper.getStudentCountData();

        // 提取两个数组
        List<String> clazzList = new ArrayList<>();
        List<Integer> dataList = new ArrayList<>();

        for (Map<String, Object> map : list) {
            clazzList.add((String) map.get("clazzName"));
            dataList.add(((Number) map.get("studentCount")).intValue());
        }

        Map<String, Object> data = new HashMap<>();
        data.put("clazzList", clazzList);
        data.put("dataList", dataList);
        return data;
    }


}
